
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第一节 损失函数 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="section2.2.html" />
    
    
    <link rel="prev" href="./" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.3.1" data-path="section2.1.html">
            
                <a href="section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="section2.2.html">
            
                <a href="section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="section2.3.html">
            
                <a href="section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="section2.4.html">
            
                <a href="section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="section2.5.html">
            
                <a href="section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/section4.1.html">
            
                <a href="../chapter4/section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/section4.2.html">
            
                <a href="../chapter4/section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第一节 损失函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E00;&#x8282;-&#x635F;&#x5931;&#x51FD;&#x6570;">&#x7B2C;&#x4E00;&#x8282; &#x635F;&#x5931;&#x51FD;&#x6570;</h1>
<ul>
<li><p>&#x795E;&#x7ECF;&#x5143;&#x6A21;&#x578B;&#xFF1A;&#x7528;&#x6570;&#x5B66;&#x516C;&#x5F0F;&#x8868;&#x793A;&#x4E3A;: <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo>(</mo><msub><mo>&#x2211;</mo><mi>i</mi></msub><msub><mi>x</mi><mi>i</mi></msub><msub><mi>w</mi><mi>i</mi></msub><mo>+</mo><mi>b</mi><mo>)</mo></mrow><annotation encoding="application/x-tex">f(\sum_ix_iw_i+b)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1.0500099999999999em;vertical-align:-0.30001em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mop"><span class="mop op-symbol small-op" style="top:-0.0000050000000000050004em;">&#x2211;</span><span class="msupsub"><span class="vlist"><span style="top:0.30001em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathit mtight">i</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathit mtight">i</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathit mtight">i</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord mathit">b</span><span class="mclose">)</span></span></span></span>&#xFF0C;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi></mrow><annotation encoding="application/x-tex">f</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10764em;">f</span></span></span></span>&#x4E3A;&#x6FC0;&#x6D3B;&#x51FD;&#x6570;&#x3002;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x662F;&#x4EE5;&#x795E;&#x7ECF;&#x5143;&#x4E3A;&#x57FA;&#x672C;&#x5355;&#x5143;&#x6784;&#x6210;&#x7684;&#x3002;</p>
</li>
<li><p>&#x6FC0;&#x6D3B;&#x51FD;&#x6570;&#xFF1A;&#x5F15;&#x5165;&#x975E;&#x7EBF;&#x6027;&#x6FC0;&#x6D3B;&#x56E0;&#x7D20;&#xFF0C;&#x63D0;&#x9AD8;&#x6A21;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x529B;&#x3002;</p>
</li>
</ul>
<blockquote>
<p>(1) &#x5E38;&#x7528;&#x7684;&#x6FC0;&#x6D3B;&#x51FD;&#x6570;<code>relu</code>&#xFF1A;&#x5728;Tensorflow&#x4E2D;&#xFF0C;&#x7528;<code>tf.nn.relu()</code>&#x8868;&#x793A;</p>
</blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo>(</mo><mi>x</mi><mo>)</mo><mo>=</mo><mi>m</mi><mi>a</mi><mi>x</mi><mo>(</mo><mi>x</mi><mo separator="true">,</mo><mn>0</mn><mo>)</mo><mo>=</mo><mrow><mo fence="true">{</mo><mtable><mtr><mtd><mrow><mn>0</mn></mrow></mtd><mtd><mrow><mi>x</mi><mo>&lt;</mo><mo>=</mo><mn>0</mn></mrow></mtd></mtr><mtr><mtd><mrow><mi>x</mi></mrow></mtd><mtd><mrow><mi>x</mi><mo>&gt;</mo><mo>=</mo><mn>0</mn></mrow></mtd></mtr></mtable></mrow></mrow><annotation encoding="application/x-tex">f(x)=max(x,0)=\begin{cases}0 &amp; x &lt;= 0 \\ x &amp; x &gt;= 0\end{cases}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.75em;"></span><span class="strut bottom" style="height:3.0000299999999998em;vertical-align:-1.25003em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathit">x</span><span class="mclose">)</span><span class="mrel">=</span><span class="mord mathit">m</span><span class="mord mathit">a</span><span class="mord mathit">x</span><span class="mopen">(</span><span class="mord mathit">x</span><span class="mpunct">,</span><span class="mord mathrm">0</span><span class="mclose">)</span><span class="mrel">=</span><span class="minner textstyle uncramped"><span class="mopen style-wrap reset-textstyle textstyle uncramped" style="top:0em;"><span class="delimsizing size4">{</span></span><span class="mord"><span class="mtable"><span class="col-align-l"><span class="vlist"><span style="top:-0.6819999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathrm">0</span></span></span><span style="top:0.7579999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit">x</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="arraycolsep" style="width:1em;"></span><span class="col-align-l"><span class="vlist"><span style="top:-0.6819999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit">x</span><span class="mrel">&lt;</span><span class="mrel">=</span><span class="mord mathrm">0</span></span></span><span style="top:0.7579999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit">x</span><span class="mrel">&gt;</span><span class="mrel">=</span><span class="mord mathrm">0</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span></p>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-07-23-15323339395898.jpg" width="200"></p>
<blockquote>
<p>(2) &#x6FC0;&#x6D3B;&#x51FD;&#x6570;<code>sigmoid</code>&#xFF1A;&#x5728;Tensorflow&#x4E2D;&#xFF0C;&#x7528;<code>tf.nn.sigmoid()</code>&#x8868;&#x793A;</p>
</blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo>(</mo><mi>x</mi><mo>)</mo><mo>=</mo><mfrac><mrow><mn>1</mn></mrow><mrow><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>&#x2212;</mo><mi>x</mi></mrow></msup></mrow></mfrac></mrow><annotation encoding="application/x-tex">f(x)=\frac{1}{1+e^{-x}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.845108em;"></span><span class="strut bottom" style="height:1.2484389999999999em;vertical-align:-0.403331em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathit">x</span><span class="mclose">)</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span><span class="mbin mtight">+</span><span class="mord mtight"><span class="mord mathit mtight">e</span><span class="msupsub"><span class="vlist"><span style="top:-0.286em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mtight">&#x2212;</span><span class="mord mathit mtight">x</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span></p>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-07-23-15323352101703.jpg" width="200"></p>
<blockquote>
<p>(3) &#x6FC0;&#x6D3B;&#x51FD;&#x6570;<code>tanh</code>&#xFF1A;&#x5728;tensorflow&#x4E2D;&#xFF0C;&#x7528;<code>tf.nn.tanh()</code>&#x8868;&#x793A;</p>
</blockquote>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo>(</mo><mi>x</mi><mo>)</mo><mo>=</mo><mfrac><mrow><mn>1</mn><mo>&#x2212;</mo><msup><mi>e</mi><mrow><mo>&#x2212;</mo><mn>2</mn><mi>x</mi></mrow></msup></mrow><mrow><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>&#x2212;</mo><mn>2</mn><mi>x</mi></mrow></msup></mrow></mfrac></mrow><annotation encoding="application/x-tex">f(x)=\frac{1-e^{-2x}}{1+e^{-2x}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.01792em;"></span><span class="strut bottom" style="height:1.4212509999999998em;vertical-align:-0.403331em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathit">x</span><span class="mclose">)</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span><span class="mbin mtight">+</span><span class="mord mtight"><span class="mord mathit mtight">e</span><span class="msupsub"><span class="vlist"><span style="top:-0.286em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mtight">&#x2212;</span><span class="mord mathrm mtight">2</span><span class="mord mathit mtight">x</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span><span class="mbin mtight">&#x2212;</span><span class="mord mtight"><span class="mord mathit mtight">e</span><span class="msupsub"><span class="vlist"><span style="top:-0.431em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle uncramped mtight"><span class="mord scriptscriptstyle uncramped mtight"><span class="mord mtight">&#x2212;</span><span class="mord mathrm mtight">2</span><span class="mord mathit mtight">x</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span></p>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-07-24-15324075893250.jpg" width="200"></p>
<ul>
<li><p>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x590D;&#x6742;&#x5EA6;&#xFF1A;&#x53EF;&#x7528;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x5C42;&#x6570;&#x548C;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#x5F85;&#x4F18;&#x5316;&#x7684;&#x53C2;&#x6570;&#x4E2A;&#x6570;&#x8868;&#x793A;</p>
</li>
<li><p>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x5C42;&#x6570;&#xFF1A;&#x4E00;&#x822C;&#x4E0D;&#x8BA1;&#x5165;&#x8F93;&#x5165;&#x5C42;&#xFF0C;&#x5C42;&#x6570;=n&#x4E2A;&#x9690;&#x85CF;&#x5C42;+1&#x4E2A;&#x8F93;&#x51FA;&#x5C42;</p>
</li>
<li><p>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x5F85;&#x4F18;&#x5316;&#x7684;&#x53C2;&#x6570;&#xFF1A;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#x6240;&#x6709;&#x53C2;&#x6570;<code>w</code>&#x7684;&#x4E2A;&#x6570;+&#x6240;&#x6709;&#x53C2;&#x6570;<code>b</code>&#x7684;&#x4E2A;&#x6570;</p>
</li>
</ul>
<blockquote>
<p>&#x4F8B;&#x5982;:</p>
</blockquote>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-07-24-15324078150692.jpg" width="200"></p>
<blockquote>
<p>&#x8BE5;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#xFF0C;&#x5305;&#x542B;1&#x4E2A;&#x8F93;&#x5165;&#x5C42;&#x3001;1&#x4E2A;&#x9690;&#x85CF;&#x5C42;&#x548C;1&#x4E2A;&#x8F93;&#x51FA;&#x5C42;&#xFF0C;&#x8BE5;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x5C42;&#x6570;&#x4E3A;2&#x5C42;&#x3002;
&#x5728;&#x8BE5;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#xFF0C;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;&#x662F;&#x6240;&#x6709;&#x53C2;&#x6570;<code>w</code>&#x7684;&#x4E2A;&#x6570;&#x52A0;&#x4E0A;&#x6240;&#x6709;&#x53C2;&#x6570;<code>b</code>&#x7684;&#x603B;&#x6570;&#xFF0C;&#x7B2C;&#x4E00;&#x5C42;&#x53C2;&#x6570;&#x7528;3&#x884C;4&#x5217;&#x7684;&#x4E8C;&#x9636;&#x5F20;&#x91CF;&#x8868;&#x793A;&#xFF08;&#x5373;12&#x4E2A;&#x7EBF;&#x4E0A;&#x7684;&#x6743;&#x91CD;<code>w</code>&#xFF09;&#x518D;&#x52A0;4&#x4E2A;&#x504F;&#x7F6E;<code>b</code>&#xFF1B;&#x7B2C;&#x4E8C;&#x5C42;&#x53C2;&#x6570;&#x662F;4&#x884C;2&#x5217;&#x7684;&#x4E8C;&#x9636;&#x5F20;&#x91CF;&#xFF08;&#x5373;8&#x4E2A;&#x7EBF;&#x4E0A;&#x7684;&#x6743;&#x91CD;<code>w</code>&#xFF09;&#x5728;&#x52A0;&#x4E0A;2&#x4E2A;&#x504F;&#x7F6E;<code>b</code>&#x3002;&#x603B;&#x53C2;&#x6570;=3X4+4+4X2+2=26</p>
</blockquote>
<ul>
<li><p>&#x635F;&#x5931;&#x51FD;&#x6570;<code>loss</code>&#xFF1A;&#x7528;&#x6765;&#x8868;&#x793A;&#x9884;&#x6D4B;&#x503C;<code>y</code>&#x4E0E;&#x5DF2;&#x77E5;&#x7B54;&#x6848;<code>y_</code>&#x7684;&#x5DEE;&#x8DDD;&#x3002;&#x5728;&#x8BAD;&#x7EC3;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x65F6;&#xFF0C;&#x901A;&#x8FC7;&#x4E0D;&#x65AD;&#x6539;&#x53D8;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#x6240;&#x6709;&#x53C2;&#x6570;&#xFF0C;&#x4F7F;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4E0D;&#x65AD;&#x51CF;&#x5C0F;&#xFF0C;&#x4ECE;&#x800C;&#x8BAD;&#x7EC3;&#x51FA;&#x66F4;&#x9AD8;&#x51C6;&#x786E;&#x7387;&#x7684;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x3002;</p>
</li>
<li><p>&#x5E38;&#x7528;&#x7684;&#x635F;&#x5931;&#x51FD;&#x6570;&#x6709;&#x5747;&#x65B9;&#x8BEF;&#x5DEE;&#x3001;&#x81EA;&#x5B9A;&#x4E49;&#x548C;&#x4EA4;&#x53C9;&#x71B5;&#x7B49;&#x3002;</p>
</li>
<li><p>&#x5747;&#x65B9;&#x8BEF;&#x5DEE;<code>mse</code>&#xFF1A;<code>n</code>&#x4E2A;&#x6837;&#x672C;&#x7684;&#x9884;&#x6D4B;&#x503C;<code>y</code>&#x4E0E;&#x5DF2;&#x77E5;&#x7B54;&#x6848;<code>y_</code>&#x7684;&#x5E73;&#x65B9;&#x548C;&#xFF0C;&#x518D;&#x6C42;&#x5747;&#x503C;&#x3002;</p>
</li>
</ul>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>M</mi><mi>S</mi><mi>E</mi><mo>(</mo><mi>y</mi><mi mathvariant="normal">_</mi><mo separator="true">,</mo><mi>y</mi><mo>)</mo><mo>=</mo><mfrac><mrow><msubsup><mo>&#x2211;</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></msubsup><mo>(</mo><mi>y</mi><mo>&#x2212;</mo><mi>y</mi><mi mathvariant="normal">_</mi><msup><mo>)</mo><mn>2</mn></msup></mrow><mrow><mi>n</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex">MSE(y\_,y) = \frac{\sum_{i=1}^n(y - y\_)^2}{n}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.158627em;"></span><span class="strut bottom" style="height:1.503627em;vertical-align:-0.345em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">M</span><span class="mord mathit" style="margin-right:0.05764em;">S</span><span class="mord mathit" style="margin-right:0.05764em;">E</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mpunct">,</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathit mtight">n</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.534707em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mop mtight"><span class="mop op-symbol small-op mtight" style="top:0.074995em;">&#x2211;</span><span class="msupsub"><span class="vlist"><span style="top:0.32101em;margin-left:0em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathit mtight">i</span><span class="mrel mtight">=</span><span class="mord mathrm mtight">1</span></span></span></span><span style="top:-0.43100000000000005em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle uncramped mtight"><span class="mord mathit mtight">n</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mopen mtight">(</span><span class="mord mathit mtight" style="margin-right:0.03588em;">y</span><span class="mbin mtight">&#x2212;</span><span class="mord mathit mtight" style="margin-right:0.03588em;">y</span><span class="mord mathrm mtight" style="margin-right:0.02778em;">_</span><span class="mclose mtight"><span class="mclose mtight">)</span><span class="msupsub"><span class="vlist"><span style="top:-0.431em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle uncramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span></p>
<blockquote>
<p>&#x5728;Tensorflow&#x4E2D;&#x7528;</p>
</blockquote>
<pre><code>loss_mse = tf.reduce_mean(tf.square(y_-y))
</code></pre><blockquote>
<p>&#x4F8B;&#x5982;&#xFF1A;&#x9884;&#x6D4B;&#x9178;&#x5976;&#x65E5;&#x9500;&#x91CF;<code>y</code>,<code>x1</code>,<code>x2</code>&#x662F;&#x5F71;&#x54CD;&#x9500;&#x91CF;&#x7684;&#x4E24;&#x4E2A;&#x56E0;&#x7D20;&#x3002;
&#x5E94;&#x63D0;&#x524D;&#x91C7;&#x96C6;&#x7684;&#x6570;&#x636E;&#x6709;&#xFF1A;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x6BCF;&#x65E5;&#x7684;<code>x1</code>&#x56E0;&#x7D20;&#x3001;<code>x2</code>&#x56E0;&#x7D20;&#x548C;&#x9500;&#x91CF;<code>y_</code>&#x3002;&#x91C7;&#x96C6;&#x7684;&#x6570;&#x636E;&#x5C3D;&#x91CF;&#x591A;&#x3002;&#x672C;&#x4F8B;&#x4E2D;&#x7528;&#x9500;&#x91CF;&#x9884;&#x6D4B;&#x4EA7;&#x91CF;&#xFF0C;&#x6700;&#x4F18;&#x7684;&#x4EA7;&#x91CF;&#x5E94;&#x8BE5;&#x7B49;&#x4E8E;&#x9500;&#x91CF;&#x3002;&#x7531;&#x4E8E;&#x76EE;&#x524D;&#x6CA1;&#x6709;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x6240;&#x4EE5;&#x62DF;&#x9020;&#x4E86;&#x4E00;&#x5957;&#x6570;&#x636E;&#x96C6;&#x3002;&#x5229;&#x7528;Tensorflow&#x4E2D;&#x51FD;&#x6570;&#x968F;&#x673A;&#x751F;&#x6210;<code>x1</code>,<code>x2</code>&#xFF0C;&#x5236;&#x9020;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_=x1+x2</code>&#xFF0C;&#x4E3A;&#x4E86;&#x66F4;&#x771F;&#x5B9E;&#xFF0C;&#x6C42;&#x548C;&#x540E;&#x8FD8;&#x52A0;&#x4E86;&#x6B63;&#x8D1F;0.05&#x7684;&#x968F;&#x673A;&#x566A;&#x58F0;&#x3002;&#x6211;&#x4EEC;&#x628A;&#x8FD9;&#x5957;&#x81EA;&#x5236;&#x7684;&#x6570;&#x636E;&#x96C6;&#x5582;&#x5165;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#xFF0C;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x4E00;&#x5C42;&#x7684;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#xFF0C;&#x62DF;&#x5408;&#x9884;&#x6D4B;&#x9178;&#x5976;&#x65E5;&#x9500;&#x91CF;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
</blockquote>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-comment">#&#x9884;&#x6D4B;&#x591A;&#x6216;&#x5C11;&#x7684;&#x5F71;&#x54CD;&#x4E00;&#x6837;</span>
<span class="hljs-comment">#0 &#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x751F;&#x6210;&#x6570;&#x636E;&#x96C6;</span>
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
BATCH_SIZE = <span class="hljs-number">8</span>
SEED = <span class="hljs-number">23455</span>

rdm = np.random.RandomState(SEED)
X = rdm.rand(<span class="hljs-number">32</span>,<span class="hljs-number">2</span>)
Y_ = [[x1+x2+(rdm.rand()/<span class="hljs-number">10.0</span><span class="hljs-number">-0.05</span>)] <span class="hljs-keyword">for</span> (x1,x2) <span class="hljs-keyword">in</span> X]

<span class="hljs-comment">#1 &#x5B9A;&#x4E49;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x8F93;&#x5165;&#x3001;&#x53C2;&#x6570;&#x548C;&#x8F93;&#x51FA;&#xFF0C;&#x5B9A;&#x4E49;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x8FC7;&#x7A0B;&#x3002;</span>
x = tf.placeholder(tf.float32, shape=(<span class="hljs-keyword">None</span>, <span class="hljs-number">2</span>))
y_ = tf.placeholder(tf.float32, shape=(<span class="hljs-keyword">None</span>, <span class="hljs-number">1</span>))
w1 = tf.Variable(tf.random_normal([<span class="hljs-number">2</span>,<span class="hljs-number">1</span>], stddev=<span class="hljs-number">1</span>, seed=<span class="hljs-number">1</span>))
y = tf.matmul(x,w1)

<span class="hljs-comment">#2 &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x53CA;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x65B9;&#x6CD5;&#x3002;</span>
<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4E3A;MSE&#xFF0C;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x65B9;&#x6CD5;&#x4E3A;&#x68AF;&#x5EA6;&#x4E0B;&#x964D;&#x3002;</span>
loss_mse = tf.reduce_mean(tf.square(y_-y))
train_step = tf.train.GradientDescentOptimizer(<span class="hljs-number">0.001</span>).minimize(loss_mse)

<span class="hljs-comment">#3 &#x751F;&#x6210;&#x4F1A;&#x8BDD;&#xFF0C;&#x8BAD;&#x7EC3;STEPS&#x8F6E;</span>
<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  init_op = tf.global_variables_initializer()
  sess.run(init_op)
  STEPS = <span class="hljs-number">20000</span>
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(STEPS):
    start = (i*BATCH_SIZE) % <span class="hljs-number">32</span>
    end = (i*BATCH_SIZE) % <span class="hljs-number">32</span> + BATCH_SIZE
    sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
    <span class="hljs-keyword">if</span> i % <span class="hljs-number">500</span> == <span class="hljs-number">0</span>:
      print(<span class="hljs-string">&quot;After %d training steps, w1 is: &quot;</span> % (i))
      print(sess.run(w1),<span class="hljs-string">&quot;\n&quot;</span>)
  print(<span class="hljs-string">&quot;Final w1 is: \n&quot;</span>, sess.run(w1))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>After 19000 training steps, w1 is:
[[0.974931  ]
 [1.02062762]]

After 19500 training steps, w1 is:
[[0.97770262]
 [1.01819491]]

Final w1 is&#xFF1A;
[[0.98019385]
 [1.0159872 ]]
</code></pre><p>&#x7531;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x53EF;&#x77E5;&#xFF0C;&#x672C;&#x4F8B;&#x4E2D;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x9884;&#x6D4B;&#x6A21;&#x578B;&#x4E3A; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mo>=</mo><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">y=w_1x_1+w_2x_2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.58333em;"></span><span class="strut bottom" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">=</span><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span> &#xFF0C;&#x635F;&#x5931;&#x51FD;&#x6570;&#x91C7;&#x7528;&#x5747;&#x65B9;&#x8BEF;&#x5DEE;&#x3002;&#x901A;&#x8FC7;&#x4F7F;&#x635F;&#x5931;&#x51FD;&#x6570;&#x503C;<code>loss</code>&#x4E0D;&#x65AD;&#x964D;&#x4F4E;&#xFF0C;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x5F97;&#x5230;&#x6700;&#x7EC8;&#x53C2;&#x6570;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>x</mi><mrow><mn>1</mn></mrow></msub><mo>=</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>9</mn><mn>8</mn><mo separator="true">,</mo><msub><mi>w</mi><mrow><mn>2</mn></mrow></msub><mo>=</mo><mn>1</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>2</mn></mrow><annotation encoding="application/x-tex">x_{1}=0.98, w_{2}=1.02</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">9</span><span class="mord mathrm">8</span><span class="mpunct">,</span><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">1</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">2</span></span></span></span>&#xFF0C;&#x9500;&#x91CF;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E3A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mo>=</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>9</mn><mn>8</mn><mo>&#x2217;</mo><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><mn>1</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>2</mn><mo>&#x2217;</mo><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">y=0.98*x_1+1.02*x_2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">=</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">9</span><span class="mord mathrm">8</span><span class="mbin">&#x2217;</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord mathrm">1</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">2</span><span class="mbin">&#x2217;</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span>&#x3002;&#x7531;&#x4E8E;&#x5728;&#x751F;&#x6210;&#x6570;&#x636E;&#x96C6;&#x65F6;&#xFF0C;&#x6807;&#x51C6;&#x7B54;&#x6848;&#x4E3A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mo>=</mo><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">y=x_1+x_2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.58333em;"></span><span class="strut bottom" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">=</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span>&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x9500;&#x91CF;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x548C;&#x6807;&#x51C6;&#x7B54;&#x6848;&#x5DF2;&#x975E;&#x5E38;&#x63A5;&#x8FD1;&#xFF0C;&#x8BF4;&#x660E;&#x8BE5;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x9884;&#x6D4B;&#x6B63;&#x786E;&#x3002;</p>
<ul>
<li>&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#xFF1A;&#x6839;&#x636E;&#x95EE;&#x9898;&#x7684;&#x5B9E;&#x9645;&#x60C5;&#x51B5;&#xFF0C;&#x5B9A;&#x5236;&#x5408;&#x7406;&#x7684;&#x635F;&#x5931;&#x51FD;&#x6570;&#x3002;</li>
</ul>
<p>&#x4F8B;&#x5982;&#xFF1A;&#x5BF9;&#x4E8E;&#x9884;&#x6D4B;&#x9178;&#x5976;&#x65E5;&#x9500;&#x91CF;&#x95EE;&#x9898;&#xFF0C;&#x5982;&#x679C;&#x9884;&#x6D4B;&#x9500;&#x91CF;&#x5927;&#x4E8E;&#x5B9E;&#x9645;&#x9500;&#x91CF;&#x4F1A;&#x635F;&#x5931;&#x6210;&#x672C;&#xFF1B;&#x5982;&#x679C;&#x9884;&#x6D4B;&#x9500;&#x91CF;&#x5C0F;&#x4E8E;&#x5B9E;&#x9645;&#x9500;&#x91CF;&#x5219;&#x4F1A;&#x635F;&#x5931;&#x5229;&#x6DA6;&#x3002;&#x5728;&#x5B9E;&#x9645;&#x751F;&#x6D3B;&#x4E2D;&#xFF0C;&#x5F80;&#x5F80;&#x5236;&#x9020;&#x4E00;&#x76D2;&#x9178;&#x5976;&#x7684;&#x6210;&#x672C;&#x548C;&#x9500;&#x552E;&#x4E00;&#x76D2;&#x9178;&#x5976;&#x7684;&#x5229;&#x6DA6;&#x662F;&#x4E0D;&#x7B49;&#x4EF7;&#x7684;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7B26;&#x5408;&#x8BE5;&#x95EE;&#x9898;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4E3A;&#xFF1A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>l</mi><mi>o</mi><mi>s</mi><mi>s</mi><mo>=</mo><msub><mo>&#x2211;</mo><mrow><mi>n</mi></mrow></msub><mi>f</mi><mo>(</mo><mi>y</mi><mi mathvariant="normal">_</mi><mo separator="true">,</mo><mi>y</mi><mo>)</mo></mrow><annotation encoding="application/x-tex">loss=\sum_{n}f(y\_,y)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1.06em;vertical-align:-0.31em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.01968em;">l</span><span class="mord mathit">o</span><span class="mord mathit">s</span><span class="mord mathit">s</span><span class="mrel">=</span><span class="mop"><span class="mop op-symbol small-op" style="top:-0.0000050000000000050004em;">&#x2211;</span><span class="msupsub"><span class="vlist"><span style="top:0.30001em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathit mtight">n</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mpunct">,</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mclose">)</span></span></span></span>
&#x5176;&#x4E2D;&#xFF0C;&#x635F;&#x5931;&#x5B9A;&#x4E49;&#x6210;&#x5206;&#x6BB5;&#x51FD;&#x6570;&#xFF1A;
<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo>(</mo><mi>y</mi><mi mathvariant="normal">_</mi><mo separator="true">,</mo><mi>y</mi><mo>)</mo><mo>=</mo><mrow><mo fence="true">{</mo><mtable><mtr><mtd><mrow><mi>P</mi><mi>R</mi><mi>O</mi><mi>F</mi><mi>I</mi><mi>T</mi><mo>&#x2217;</mo><mo>(</mo><mi>y</mi><mi mathvariant="normal">_</mi><mo>&#x2212;</mo><mi>y</mi><mo>)</mo></mrow></mtd><mtd><mrow><mi>y</mi><mo>&lt;</mo><mi>y</mi><mi mathvariant="normal">_</mi></mrow></mtd></mtr><mtr><mtd><mrow><mi>C</mi><mi>O</mi><mi>S</mi><mi>T</mi><mo>&#x2217;</mo><mo>(</mo><mi>y</mi><mo>&#x2212;</mo><mi>y</mi><mi mathvariant="normal">_</mi><mo>)</mo></mrow></mtd><mtd><mrow><mi>y</mi><mo>&#x2265;</mo><mi>y</mi><mi mathvariant="normal">_</mi></mrow></mtd></mtr></mtable></mrow></mrow><annotation encoding="application/x-tex">f(y\_,y)=\begin{cases}PROFIT*(y\_-y) &amp; y &lt; y\_\\ COST*(y-y\_) &amp; y \geq y\_\end{cases}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.75em;"></span><span class="strut bottom" style="height:3.0000299999999998em;vertical-align:-1.25003em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mpunct">,</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mrel">=</span><span class="minner textstyle uncramped"><span class="mopen style-wrap reset-textstyle textstyle uncramped" style="top:0em;"><span class="delimsizing size4">{</span></span><span class="mord"><span class="mtable"><span class="col-align-l"><span class="vlist"><span style="top:-0.6819999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit" style="margin-right:0.13889em;">P</span><span class="mord mathit" style="margin-right:0.00773em;">R</span><span class="mord mathit" style="margin-right:0.02778em;">O</span><span class="mord mathit" style="margin-right:0.13889em;">F</span><span class="mord mathit" style="margin-right:0.07847em;">I</span><span class="mord mathit" style="margin-right:0.13889em;">T</span><span class="mbin">&#x2217;</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mbin">&#x2212;</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mclose">)</span></span></span><span style="top:0.7579999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit" style="margin-right:0.07153em;">C</span><span class="mord mathit" style="margin-right:0.02778em;">O</span><span class="mord mathit" style="margin-right:0.05764em;">S</span><span class="mord mathit" style="margin-right:0.13889em;">T</span><span class="mbin">&#x2217;</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mbin">&#x2212;</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mclose">)</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="arraycolsep" style="width:1em;"></span><span class="col-align-l"><span class="vlist"><span style="top:-0.6819999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">&lt;</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span></span></span><span style="top:0.7579999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="mord textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">&#x2265;</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span>
&#x635F;&#x5931;&#x51FD;&#x6570;&#x8868;&#x793A;&#xFF0C;&#x82E5;&#x9884;&#x6D4B;&#x7ED3;&#x679C;<code>y</code>&#x5C0F;&#x4E8E;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_</code>&#xFF0C;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4E3A;&#x5229;&#x6DA6;&#x4E58;&#x4EE5;&#x9884;&#x6D4B;&#x7ED3;&#x679C;<code>y</code>&#x4E0E;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_</code>&#x4E4B;&#x5DEE;&#xFF1B;&#x82E5;&#x9884;&#x6D4B;&#x7ED3;&#x679C;<code>y</code>&#x5927;&#x4E8E;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_</code>&#xFF0C;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4E3A;&#x6210;&#x672C;&#x4E58;&#x4EE5;&#x9884;&#x6D4B;&#x7ED3;&#x679C;<code>y</code>&#x4E0E;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_</code>&#x4E4B;&#x5DEE;&#x3002;</p>
<p>&#x7528;Tensorflow&#x51FD;&#x6570;&#x8868;&#x793A;&#x4E3A;:</p>
<pre><code>loss = tf.reduce_sum(tf.where(tf.greater(y,y_), COST*(y-y_), PROFIT*(y_,y)))
</code></pre><blockquote>
<p>(1)&#x82E5;&#x9178;&#x5976;&#x6210;&#x672C;&#x4E3A;1&#x5143;&#xFF0C;&#x9178;&#x5976;&#x9500;&#x552E;&#x5229;&#x6DA6;&#x4E3A;9&#x5143;&#xFF0C;&#x5219;&#x5236;&#x9020;&#x6210;&#x672C;&#x5C0F;&#x4E8E;&#x9178;&#x5976;&#x5229;&#x6DA6;&#xFF0C;&#x56E0;&#x6B64;&#x5E0C;&#x671B;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;<code>y</code>&#x591A;&#x4E00;&#x4E9B;&#x3002;&#x91C7;&#x7528;&#x4E0A;&#x8FF0;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#xFF0C;&#x8BAD;&#x7EC3;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x3002;</p>
</blockquote>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-comment">#&#x9178;&#x5976;&#x6210;&#x672C;1&#x5143;&#xFF0C;&#x9178;&#x5976;&#x5229;&#x6DA6;9&#x5143;</span>
<span class="hljs-comment">#&#x9884;&#x6D4B;&#x5C11;&#x4E86;&#x635F;&#x5931;&#xFF0C;&#x6545;&#x4E0D;&#x8981;&#x9884;&#x6D4B;&#x5C11;&#xFF0C;&#x6545;&#x751F;&#x6210;&#x7684;&#x6A21;&#x578B;&#x4F1A;&#x591A;&#x9884;&#x6D4B;&#x4E00;&#x4E9B;</span>
<span class="hljs-comment">#0 &#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x751F;&#x6210;&#x6570;&#x636E;&#x96C6;</span>
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
BATCH_SIZE = <span class="hljs-number">8</span>
SEED = <span class="hljs-number">23455</span>
COST = <span class="hljs-number">1</span>
PROFIT = <span class="hljs-number">9</span>

rdm = np.random.RandomState(SEED)
X = rdm.rand(<span class="hljs-number">32</span>,<span class="hljs-number">2</span>)
Y_ = [[x1+x2+(rdm.rand()/<span class="hljs-number">10.0</span><span class="hljs-number">-0.05</span>)] <span class="hljs-keyword">for</span> (x1,x2) <span class="hljs-keyword">in</span> X]

<span class="hljs-comment">#1 &#x5B9A;&#x4E49;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x8F93;&#x5165;&#x3001;&#x53C2;&#x6570;&#x548C;&#x8F93;&#x51FA;&#xFF0C;&#x5B9A;&#x4E49;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x8FC7;&#x7A0B;&#x3002;</span>
x = tf.placeholder(tf.float32, shape=(<span class="hljs-keyword">None</span>, <span class="hljs-number">2</span>))
y_ = tf.placeholder(tf.float32, shape=(<span class="hljs-keyword">None</span>, <span class="hljs-number">1</span>))
w1 = tf.Variable(tf.random_normal([<span class="hljs-number">2</span>,<span class="hljs-number">1</span>], stddev=<span class="hljs-number">1</span>, seed=<span class="hljs-number">1</span>))
y = tf.matmul(x,w1)

<span class="hljs-comment">#2 &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x53CA;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x65B9;&#x6CD5;&#x3002;</span>
<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4F7F;&#x5F97;&#x9884;&#x6D4B;&#x5C11;&#x4E86;&#x7684;&#x635F;&#x5931;&#x5927;&#xFF0C;&#x4E8E;&#x662F;&#x6A21;&#x578B;&#x5E94;&#x8BE5;&#x504F;&#x5411;&#x591A;&#x7684;&#x65B9;&#x5411;&#x9884;&#x6D4B;&#x3002;</span>
loss = tf.reduce_sum(tf.where(tf.greater(y,y_),(y-y_)*COST,(y_-y)*PROFIT))
train_step = tf.train.GradientDescentOptimizer(<span class="hljs-number">0.001</span>).minimize(loss)

<span class="hljs-comment">#3 &#x751F;&#x6210;&#x4F1A;&#x8BDD;&#xFF0C;&#x8BAD;&#x7EC3;STEPS&#x8F6E;</span>
<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  init_op = tf.global_variables_initializer()
  sess.run(init_op)
  STEPS = <span class="hljs-number">20000</span>
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(STEPS):
    start = (i*BATCH_SIZE) % <span class="hljs-number">32</span>
    end = (i*BATCH_SIZE) % <span class="hljs-number">32</span> + BATCH_SIZE
    sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
    <span class="hljs-keyword">if</span> i % <span class="hljs-number">500</span> == <span class="hljs-number">0</span>:
      print(<span class="hljs-string">&quot;After %d training steps, w1 is: &quot;</span> % (i))
      print(sess.run(w1),<span class="hljs-string">&quot;\n&quot;</span>)
  print(<span class="hljs-string">&quot;Final w1 is: \n&quot;</span>, sess.run(w1))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>After 2000 training steps, w1 is:
[[1.01793861]
 [1.04128993]]

After 2500 training steps, w1 is:
[[1.02059376]
 [1.03906775]]

Final w1 is:
[[1.02965927]
 [1.0484432 ]]
</code></pre><p>&#x7531;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x53EF;&#x77E5;&#xFF0C;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6700;&#x7EC8;&#x53C2;&#x6570;&#x4E3A; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub><mo>=</mo><mn>1</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>3</mn></mrow><annotation encoding="application/x-tex">w_1=1.03</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">1</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">3</span></span></span></span>&#xFF0C;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>w</mi><mn>2</mn></msub><mo>=</mo><mn>1</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>5</mn></mrow><annotation encoding="application/x-tex">w_2=1.05</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">1</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">5</span></span></span></span>&#xFF0C;&#x9500;&#x91CF;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E3A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mo>=</mo><mn>1</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>3</mn><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><mn>1</mn><mi mathvariant="normal">.</mi><mn>0</mn><mn>5</mn><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">y =1.03x_1 + 1.05x_2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">=</span><span class="mord mathrm">1</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">3</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord mathrm">1</span><span class="mord mathrm">.</span><span class="mord mathrm">0</span><span class="mord mathrm">5</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span>&#x3002;&#x7531;&#x6B64;&#x53EF;&#x89C1;&#xFF0C;&#x91C7;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#x5927;&#x4E8E;&#x91C7;&#x7528;&#x5747;&#x65B9;&#x8BEF;&#x5DEE;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x66F4;&#x7B26;&#x5408;&#x5B9E;&#x9645;&#x9700;&#x6C42;&#x3002;</p>
<blockquote>
<p>(2)&#x82E5;&#x9178;&#x5976;&#x6210;&#x672C;&#x4E3A;9&#x5143;&#xFF0C;&#x9178;&#x5976;&#x9500;&#x552E;&#x5229;&#x6DA6;&#x4E3A;1&#x5143;&#xFF0C;&#x5219;&#x5236;&#x9020;&#x6210;&#x672C;&#x5927;&#x4E8E;&#x9178;&#x5976;&#x5229;&#x6DA6;&#xFF0C;&#x56E0;&#x6B64;&#x5E0C;&#x671B;&#x9884;&#x6D4B;&#x7ED3;&#x679C;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi></mrow><annotation encoding="application/x-tex">y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.43056em;"></span><span class="strut bottom" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span></span></span></span>&#x5C0F;&#x4E00; &#x4E9B;&#x3002;&#x91C7;&#x7528;&#x4E0A;&#x8FF0;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#xFF0C;&#x8BAD;&#x7EC3;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x3002;
&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-comment">#&#x9178;&#x5976;&#x6210;&#x672C;1&#x5143;&#xFF0C;&#x9178;&#x5976;&#x5229;&#x6DA6;9&#x5143;</span>
<span class="hljs-comment">#&#x9884;&#x6D4B;&#x5C11;&#x4E86;&#x635F;&#x5931;&#xFF0C;&#x6545;&#x4E0D;&#x8981;&#x9884;&#x6D4B;&#x5C11;&#xFF0C;&#x6545;&#x751F;&#x6210;&#x7684;&#x6A21;&#x578B;&#x4F1A;&#x591A;&#x9884;&#x6D4B;&#x4E00;&#x4E9B;</span>
<span class="hljs-comment">#0 &#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x751F;&#x6210;&#x6570;&#x636E;&#x96C6;</span>
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
BATCH_SIZE = <span class="hljs-number">8</span>
SEED = <span class="hljs-number">23455</span>
COST = <span class="hljs-number">9</span>
PROFIT = <span class="hljs-number">1</span>

rdm = np.random.RandomState(SEED)
X = rdm.rand(<span class="hljs-number">32</span>,<span class="hljs-number">2</span>)
Y_ = [[x1+x2+(rdm.rand()/<span class="hljs-number">10.0</span><span class="hljs-number">-0.05</span>)] <span class="hljs-keyword">for</span> (x1,x2) <span class="hljs-keyword">in</span> X]

<span class="hljs-comment">#1 &#x5B9A;&#x4E49;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x8F93;&#x5165;&#x3001;&#x53C2;&#x6570;&#x548C;&#x8F93;&#x51FA;&#xFF0C;&#x5B9A;&#x4E49;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x8FC7;&#x7A0B;&#x3002;</span>
x = tf.placeholder(tf.float32, shape=(<span class="hljs-keyword">None</span>, <span class="hljs-number">2</span>))
y_ = tf.placeholder(tf.float32, shape=(<span class="hljs-keyword">None</span>, <span class="hljs-number">1</span>))
w1 = tf.Variable(tf.random_normal([<span class="hljs-number">2</span>,<span class="hljs-number">1</span>], stddev=<span class="hljs-number">1</span>, seed=<span class="hljs-number">1</span>))
y = tf.matmul(x,w1)

<span class="hljs-comment">#2 &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x53CA;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x65B9;&#x6CD5;&#x3002;</span>
<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4F7F;&#x5F97;&#x9884;&#x6D4B;&#x591A;&#x4E86;&#x7684;&#x635F;&#x5931;&#x5927;&#xFF0C;&#x4E8E;&#x662F;&#x6A21;&#x578B;&#x5E94;&#x8BE5;&#x504F;&#x5411;&#x5C11;&#x7684;&#x65B9;&#x5411;&#x9884;&#x6D4B;&#x3002;</span>
loss = tf.reduce_sum(tf.where(tf.greater(y,y_),(y-y_)*COST,(y_-y)*PROFIT))
train_step = tf.train.GradientDescentOptimizer(<span class="hljs-number">0.001</span>).minimize(loss)

<span class="hljs-comment">#3 &#x751F;&#x6210;&#x4F1A;&#x8BDD;&#xFF0C;&#x8BAD;&#x7EC3;STEPS&#x8F6E;</span>
<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  init_op = tf.global_variables_initializer()
  sess.run(init_op)
  STEPS = <span class="hljs-number">20000</span>
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(STEPS):
    start = (i*BATCH_SIZE) % <span class="hljs-number">32</span>
    end = (i*BATCH_SIZE) % <span class="hljs-number">32</span> + BATCH_SIZE
    sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
    <span class="hljs-keyword">if</span> i % <span class="hljs-number">500</span> == <span class="hljs-number">0</span>:
      print(<span class="hljs-string">&quot;After %d training steps, w1 is: &quot;</span> % (i))
      print(sess.run(w1),<span class="hljs-string">&quot;\n&quot;</span>)
  print(<span class="hljs-string">&quot;Final w1 is: \n&quot;</span>, sess.run(w1))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;:</p>
<pre><code>After 2000 training steps, w1 is:
[[0.96024752]
 [0.97420841]]

After 2500 training steps, w1 is:
[[0.96100295]
 [0.96993417]]

Final w1 is
[[0.96004069]
 [0.97334176]]
</code></pre><p>&#x7531;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x53EF;&#x77E5;&#xFF0C;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6700;&#x7EC8;&#x53C2;&#x6570;&#x4E3A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub><mo>=</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>9</mn><mn>6</mn></mrow><annotation encoding="application/x-tex">w_1=0.96</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">9</span><span class="mord mathrm">6</span></span></span></span>&#xFF0C;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>w</mi><mn>2</mn></msub><mo>=</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>9</mn><mn>7</mn></mrow><annotation encoding="application/x-tex">w_2=0.97</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit" style="margin-right:0.02691em;">w</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.02691em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">=</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">9</span><span class="mord mathrm">7</span></span></span></span>&#xFF0C;&#x9500;&#x91CF;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E3A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mo>=</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>9</mn><mn>6</mn><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><mn>0</mn><mi mathvariant="normal">.</mi><mn>9</mn><mn>7</mn><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">y=0.96x_1 + 0.97x_2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mrel">=</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">9</span><span class="mord mathrm">6</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord mathrm">0</span><span class="mord mathrm">.</span><span class="mord mathrm">9</span><span class="mord mathrm">7</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathrm mtight">2</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span>&#x3002; &#x56E0;&#x6B64;&#xFF0C;&#x91C7;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#x5C0F;&#x4E8E;&#x91C7;&#x7528;&#x5747;&#x65B9;&#x8BEF;&#x5DEE;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x66F4;&#x7B26;&#x5408;&#x5B9E;&#x9645;&#x9700;&#x6C42;&#x3002;</p>
<ul>
<li>&#x4EA4;&#x53C9;&#x71B5;<code>Cross Entropy</code>:&#x8868;&#x793A;&#x4E24;&#x4E2A;&#x6982;&#x7387;&#x5206;&#x5E03;&#x4E4B;&#x95F4;&#x7684;&#x8DDD;&#x79BB;&#x3002;&#x4EA4;&#x53C9;&#x71B5;&#x8D8A;&#x5927;&#xFF0C;&#x4E24;&#x4E2A;&#x6982;&#x7387;&#x5206;&#x5E03;&#x8DDD;&#x79BB;&#x8D8A;&#x8FDC;&#xFF0C;&#x4E24;&#x4E2A;&#x6982;&#x7387;&#x5206;&#x5E03;&#x8D8A;&#x76F8;&#x5F02;;&#x4EA4;&#x53C9;&#x71B5;&#x8D8A;&#x5C0F;&#xFF0C;&#x4E24;&#x4E2A;&#x6982;&#x7387;&#x5206;&#x5E03;&#x8DDD;&#x79BB;&#x8D8A;&#x8FD1;&#xFF0C;&#x4E24;&#x4E2A;&#x6982;&#x7387;&#x5206;&#x5E03;&#x8D8A;&#x76F8;&#x4F3C;&#x3002; &#x4EA4;&#x53C9;&#x71B5;&#x8BA1;&#x7B97;&#x516C;&#x5F0F;:
<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>H</mi><mo>(</mo><mi>y</mi><mi mathvariant="normal">_</mi><mo separator="true">,</mo><mi>y</mi><mo>)</mo><mo>=</mo><mo>&#x2212;</mo><mo>&#x2211;</mo><mrow><mi>y</mi><mi mathvariant="normal">_</mi><mi>log</mi><mrow><mi>y</mi></mrow></mrow></mrow><annotation encoding="application/x-tex">H(y\_,y)=-\sum{y\_\log{y}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1.06em;vertical-align:-0.31em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.08125em;">H</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mpunct">,</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mrel">=</span><span class="mord">&#x2212;</span><span class="mop op-symbol small-op" style="top:-0.0000050000000000050004em;">&#x2211;</span><span class="mord textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mord textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span></span></span></span></span></span></li>
</ul>
<p>&#x7528; Tensorflow &#x51FD;&#x6570;&#x8868;&#x793A;&#x4E3A;</p>
<pre><code>ce = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-12,1.0)))
</code></pre><p>&#x4F8B;&#x5982;:&#x4E24;&#x4E2A;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x89E3;&#x51B3;&#x4E8C;&#x5206;&#x7C7B;&#x95EE;&#x9898;&#x4E2D;&#xFF0C;&#x5DF2;&#x77E5;&#x6807;&#x51C6;&#x7B54;&#x6848;&#x4E3A;<code>y_=(1,0)</code>&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E3A;<code>y1=(0.6,0.4)</code>&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E3A;<code>y2=(0.8,0.2)</code>&#xFF0C;&#x5224;&#x65AD;&#x54EA;&#x4E2A;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x9884;&#x6D4B;&#x7684;&#x7ED3;&#x679C;&#x66F4;&#x63A5;&#x8FD1;&#x6807;&#x51C6;&#x7B54;&#x6848;&#xFF1F;</p>
<p>&#x6839;&#x636E;&#x4EA4;&#x53C9;&#x71B5;&#x7684;&#x8BA1;&#x7B97;&#x516C;&#x5F0F;&#x5F97;:</p>
<p><code>H1((1,0),(0.6,0.4)) = -(1*log0.6 + 0*log0.4) &#x2248; -(-0.222 + 0) = 0.222</code></p>
<p><code>H2((1,0),(0.8,0.2)) = -(1*log0.8 + 0*log0.2) &#x2248; -(-0.097 + 0) = 0.097</code></p>
<p>&#x7531;&#x4E8E;<code>0.222 &gt; 0.097</code>&#xFF0C;&#x6240;&#x4EE5;&#x9884;&#x6D4B;&#x7ED3;&#x679C;<code>y2</code>&#x4E0E;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_</code>&#x66F4;&#x63A5;&#x8FD1;&#xFF0C;<code>y2</code>&#x9884;&#x6D4B;&#x66F4;&#x51C6;&#x786E;&#x3002;</p>
<ul>
<li>Softmax&#x51FD;&#x6570;&#xFF1A;&#x5C06;<code>n</code>&#x5206;&#x7C7B;&#x7684;<code>n</code>&#x4E2A;&#x8F93;&#x51FA;<code>(y1,y2,...,yn)</code>&#x53D8;&#x4E3A;&#x6EE1;&#x8DB3;&#x4EE5;&#x4E0B;&#x6982;&#x7387;&#x5206;&#x5E03;&#x8981;&#x6C42;&#x7684;&#x51FD;&#x6570;&#x3002;</li>
</ul>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">&#x2200;</mi><mrow><mi>x</mi></mrow></mrow><annotation encoding="application/x-tex">\forall{x}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:0.69444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">&#x2200;</span><span class="mord textstyle uncramped"><span class="mord mathit">x</span></span></span></span></span> &#x6709; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>P</mi><mo>(</mo><mi>X</mi><mo>=</mo><mi>x</mi><mo>)</mo><mo>&#x2208;</mo><mo>[</mo><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo>]</mo></mrow><annotation encoding="application/x-tex">P(X=x)\in[0,1]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.13889em;">P</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.07847em;">X</span><span class="mrel">=</span><span class="mord mathit">x</span><span class="mclose">)</span><span class="mrel">&#x2208;</span><span class="mopen">[</span><span class="mord mathrm">0</span><span class="mpunct">,</span><span class="mord mathrm">1</span><span class="mclose">]</span></span></span></span> &#x4E14; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>&#x2211;</mo><mrow><msub><mi>P</mi><mi>x</mi></msub><mo>(</mo><mi>X</mi><mo>=</mo><mi>x</mi><mo>)</mo></mrow><mo>=</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\sum{P_x(X=x)}=1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1.00001em;vertical-align:-0.25001em;"></span><span class="base textstyle uncramped"><span class="mop op-symbol small-op" style="top:-0.0000050000000000050004em;">&#x2211;</span><span class="mord textstyle uncramped"><span class="mord"><span class="mord mathit" style="margin-right:0.13889em;">P</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.13889em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathit mtight">x</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.07847em;">X</span><span class="mrel">=</span><span class="mord mathit">x</span><span class="mclose">)</span></span><span class="mrel">=</span><span class="mord mathrm">1</span></span></span></span></p>
<p>softmax&#x51FD;&#x6570;&#x8868;&#x793A;&#x4E3A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>s</mi><mi>o</mi><mi>f</mi><mi>t</mi><mi>m</mi><mi>a</mi><mi>x</mi><mo>(</mo><msub><mi>y</mi><mi>i</mi></msub><mo>)</mo><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mrow><msub><mi>y</mi><mrow><mi>i</mi></mrow></msub></mrow></msup></mrow><mrow><msubsup><mo>&#x2211;</mo><mrow><mi>j</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></msubsup><msup><mi>e</mi><mrow><msub><mi>y</mi><mrow><mi>i</mi></mrow></msub></mrow></msup></mrow></mfrac></mrow><annotation encoding="application/x-tex">softmax(y_i)=\frac{e^{y_{i}}}{\sum_{j=1}^{n}e^{y_{i}}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.91098em;"></span><span class="strut bottom" style="height:1.577907em;vertical-align:-0.6669269999999999em;"></span><span class="base textstyle uncramped"><span class="mord mathit">s</span><span class="mord mathit">o</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">t</span><span class="mord mathit">m</span><span class="mord mathit">a</span><span class="mord mathit">x</span><span class="mopen">(</span><span class="mord"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:-0.03588em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord mathit mtight">i</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mclose">)</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mop mtight"><span class="mop op-symbol small-op mtight" style="top:0.074995em;">&#x2211;</span><span class="msupsub"><span class="vlist"><span style="top:0.32101em;margin-left:0em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathit mtight" style="margin-right:0.05724em;">j</span><span class="mrel mtight">=</span><span class="mord mathrm mtight">1</span></span></span></span><span style="top:-0.397em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathit mtight">n</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord mtight"><span class="mord mathit mtight">e</span><span class="msupsub"><span class="vlist"><span style="top:-0.33255em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mtight"><span class="mord mathit mtight" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist"><span style="top:0.31472em;margin-right:0.1em;margin-left:-0.03588em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptscriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathit mtight">i</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mtight"><span class="mord mathit mtight">e</span><span class="msupsub"><span class="vlist"><span style="top:-0.431em;margin-right:0.07142857142857144em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle uncramped mtight"><span class="mord scriptscriptstyle uncramped mtight"><span class="mord mtight"><span class="mord mathit mtight" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist"><span style="top:0.31472em;margin-right:0.1em;margin-left:-0.03588em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptscriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathit mtight">i</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span></p>
<p>softmax&#x51FD;&#x6570;&#x5E94;&#x7528;&#xFF1A;&#x5728;<code>n</code>&#x5206;&#x7C7B;&#x4E2D;&#xFF0C;&#x6A21;&#x578B;&#x4F1A;&#x6709;<code>n</code>&#x4E2A;&#x8F93;&#x51FA;&#xFF0C;&#x5373;y1,y2,...,yn&#xFF0C;&#x5176;&#x4E2D;yi&#x8868;&#x793A;&#x7B2C;i&#x4E2D;&#x60C5;&#x51B5;&#x7684;&#x53EF;&#x80FD;&#x6027;&#x5927;&#x5C0F;&#x3002;&#x5C06;<code>n</code>&#x4E2A;&#x8F93;&#x51FA;&#x7ECF;&#x8FC7;softmax&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x5F97;&#x5230;&#x7B26;&#x5408;&#x6982;&#x7387;&#x5206;&#x5E03;&#x7684;&#x5206;&#x7C7B;&#x7ED3;&#x679C;&#x3002;</p>
<ul>
<li>&#x5728;Tensorflow&#x4E2D;&#xFF0C;&#x4E00;&#x822C;&#x8BA9;&#x6A21;&#x578B;&#x7ECF;&#x8FC7;softmax&#x51FD;&#x6570;&#xFF0C;&#x4EE5;&#x83B7;&#x5F97;&#x8F93;&#x51FA;&#x5206;&#x7C7B;&#x7684;&#x6982;&#x7387;&#x5206;&#x5E03;&#xFF0C;&#x518D;&#x4E0E;&#x6807;&#x51C6;&#x7B54;&#x6848;&#x5BF9;&#x6BD4;&#xFF0C;&#x6C42;&#x51FA;&#x4EA4;&#x53C9;&#x71B5;&#xFF0C;&#x5F97;&#x5230;&#x635F;&#x5931;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x5982;&#x4E0B;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#xFF1A;</li>
</ul>
<pre><code>ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1))
cem = tf.reduce_mean(ce)
</code></pre><footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-09-18 19:09:43
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: 第二章 神经网络优化">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="section2.2.html" class="navigation navigation-next " aria-label="Next page: 第二节 学习率">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第一节 损失函数","level":"1.3.1","depth":2,"next":{"title":"第二节 学习率","level":"1.3.2","depth":2,"path":"chapter2/section2.2.md","ref":"chapter2/section2.2.md","articles":[]},"previous":{"title":"第二章 神经网络优化","level":"1.3","depth":1,"path":"chapter2/README.md","ref":"chapter2/README.md","articles":[{"title":"第一节 损失函数","level":"1.3.1","depth":2,"path":"chapter2/section2.1.md","ref":"chapter2/section2.1.md","articles":[]},{"title":"第二节 学习率","level":"1.3.2","depth":2,"path":"chapter2/section2.2.md","ref":"chapter2/section2.2.md","articles":[]},{"title":"第三节 滑动平均","level":"1.3.3","depth":2,"path":"chapter2/section2.3.md","ref":"chapter2/section2.3.md","articles":[]},{"title":"第四节 正则化","level":"1.3.4","depth":2,"path":"chapter2/section2.4.md","ref":"chapter2/section2.4.md","articles":[]},{"title":"第五节 神经网络的搭建","level":"1.3.5","depth":2,"path":"chapter2/section2.5.md","ref":"chapter2/section2.5.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter2/section2.1.md","mtime":"2018-09-18T11:09:43.340Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

